package com.wenyu.wenyuyuedu.fragment.homepagefragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.jingchen.pulltorefresh.PullableListView;
import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.homepage.MyAttentionActivity;
import com.wenyu.wenyuyuedu.adapter.homepagefragment.HomeAllAdapter;
import com.wenyu.wenyuyuedu.bean.homepager.Homepage_All_item;
import com.wenyu.wenyuyuedu.global.ApiWrapper;
import com.wenyu.wenyuyuedu.server.homepager.Homepage_All_Server;
import com.wenyu.wenyuyuedu.utils.AbSharedUtil;
import com.wenyu.wenyuyuedu.utils.MyToast;
import com.wenyu.wenyuyuedu.utils.SharedConstants;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 * 首页--全部
 */
public class AllFragment extends Fragment {
    @InjectView(R.id.content_view)
    PullableListView contentView;
    @InjectView(R.id.refresh_view)
    PullToRefreshLayout refreshView;
    @InjectView(R.id.iv_defult)
    ImageView ivDefult;
    private View allFragmentView;
    private String token;
    private int page = 1;
    private int judge = 1;
    private List<Homepage_All_item.InfoBean> infoList;
    private List<Homepage_All_item.InfoBean> mCurrent = new ArrayList<>();
    private List<Homepage_All_item.InfoBean> infoListAll = new ArrayList<>();
    private boolean isLoading = false;
    private ListView listView;
    private String nickname;
    private String create_time;
    private String content;
    private List<String> images;
    private HomeAllAdapter homeAllAdapter;

    public AllFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        allFragmentView = inflater.inflate(R.layout.fragment_all, container, false);
        ButterKnife.inject(this, allFragmentView);
        homeAllAdapter = new HomeAllAdapter(getActivity());
        contentView.setAdapter(homeAllAdapter);
        listView = (ListView) refreshView.getPullableView();
        refreshView.setOnPullListener(new PullToRefreshLayout.OnPullListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                new Handler() {
                    public void handleMessage(Message msg) {
                        page = 1;
                        isLoading = false;
                        requesData();
                        //千万别忘了告诉控件刷新完毕了
                        refreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                }.sendEmptyMessageDelayed(0, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                new Handler() {
                    public void handleMessage(Message msg) {
                        page++;
                        isLoading = true;
                        requesData();
                        //千万别忘了告诉控件刷新完毕了
                        refreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                }.sendEmptyMessageDelayed(0, 1000);
            }
        });
        contentView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                intent.setClass(getContext(), MyAttentionActivity.class);
                Homepage_All_item.InfoBean infoBean = infoListAll.get(i);
                String title = infoBean.getTitle();
                String id = infoBean.getId();
                String nickname = infoBean.getNickname();
                intent.putExtra("allattentionnickname", nickname);
                intent.putExtra("allattentionid", id);
                intent.putExtra("allattentiontitle", title);
                startActivity(intent);
            }
        });
        return allFragmentView;
    }

    @Override
    public void onStart() {
        super.onStart();
        isLoading = false;
        requesData();
    }

    private void requesData() {
        token = AbSharedUtil.getString(getActivity(), SharedConstants.USER_TOKEN);
        Homepage_All_Server homepage_all_server = ApiWrapper.getInstance().create(Homepage_All_Server.class);
        Call<Homepage_All_item> homepageAll = homepage_all_server.getHomepageAll(token, page + "", judge + "");
        homepageAll.enqueue(new Callback<Homepage_All_item>() {
            @Override
            public void onResponse(Call<Homepage_All_item> call, Response<Homepage_All_item> response) {
                Homepage_All_item body = response.body();
                if (body == null) {
                    ivDefult.setVisibility(View.VISIBLE);
                    return;
                } else {
                    int code = body.getCode();
                    if (code == 1) {
                        if (body.getInfo() != null) {
                            infoList = body.getInfo();
                            if (!isLoading) {
                                infoListAll.clear();
                            }
                            infoListAll.addAll(infoList);
                            //infoList.addAll(infoList);
                            homeAllAdapter.addAll(infoListAll);
                            ivDefult.setVisibility(View.INVISIBLE);
                        } else {
                            ivDefult.setVisibility(View.VISIBLE);
                        }
                    } else if (code == 2100) {
                        //所有帖子已经加载完成
                        MyToast.showToast(getActivity(), "没有更多数据了", Toast.LENGTH_SHORT);
                    }
                }
            }

            @Override
            public void onFailure(Call<Homepage_All_item> call, Throwable t) {
                System.out.println("AllFragment: " + t);
                ivDefult.setVisibility(View.VISIBLE);
                MyToast.showToast(getActivity(), "网络异常，请稍后重试", Toast.LENGTH_SHORT);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }
}
